package com.myl.service.system.impl;

import java.util.List;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myl.dao.system.DeptDao;
import com.myl.domain.system.Dept;
import com.myl.factory.MapperFactory;
import com.myl.service.system.DeptService;
import com.myl.utils.TransactionUtil;

public class DeptServiceImpl implements DeptService{

	@Override
	public void save(Dept dept) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			dept.setId(id);
			deptDao.save(dept);
			
			TransactionUtil.commit(sqlSession);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public void delete(Dept dept) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);		
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			dept.setId(id);
			
			deptDao.delete(dept);
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public void update(Dept dept) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);		
			deptDao.update(dept);
			
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public Dept findById(String id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);		
			return deptDao.findById(id);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public List<Dept> findAll() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);		
			
			List<Dept> all = deptDao.findAll();
			return all;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public PageInfo findAll(int page, int size) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);		
			
			PageHelper.startPage(page,size);
			List<Dept> all = deptDao.findAll();
			PageInfo  pageInfo = new PageInfo(all);
			return pageInfo;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	
}
